package com.chunlin.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/status")
public class StatusServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String statusCode = req.getParameter("status");
        if (statusCode != null) {
            if ("500".equals(statusCode)) {
                //通过SendError的方式,可以将Tomcat的内置错误码页面也返回给浏览器
                resp.sendError(500);
            } else if ("404".equals(statusCode)) {
                resp.sendError(404);
            } else {
                //这个只是单纯的设置了状态码,并没有将Tomcat内置的状态码页面也返回给浏览器
                //浏览器接收到状态码之后,自己使用浏览器内置的状态码页面
                resp.setStatus(Integer.parseInt(statusCode));
            }
        }
    }
}
